package models

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	err error
	DB  *gorm.DB
	Rdb *redis.Client
	Ctx = context.Background()
)

func MysqlInit() {
	dsn := "root:root@tcp(127.0.0.1:3306)/ceshi0512?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}
	DB.AutoMigrate(&User{})
}

func RedisInit() {
	Rdb = redis.NewClient(&redis.Options{
		Addr:     "117.50.173.206:6379",
		Password: "Tyq2213353543", // 没有密码，默认值
		DB:       0,               // 默认DB 0
	})
	_, err = Rdb.Ping(Ctx).Result()
	if err != nil {
		fmt.Println("redis连接失败")
		return
	}
}
